package com.otod.pm.prj.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.otod.base.exception.AppException;
import com.otod.base.json.JsonFailure;
import com.otod.base.json.JsonSuccess;
import com.otod.commons.utils.DateUtils;
import com.otod.commons.utils.StringUtils;
import com.otod.commons.web.BaseController;
import com.otod.pm.constants.PmConstant;
import com.otod.pm.prj.bo.StepBo;
import com.otod.pm.prj.service.ProjectService;
import com.otod.pm.prj.service.StepService;
import com.otod.pm.prj.service.TaskService;
import com.otod.pm.prj.vo.ProjectVo;
import com.otod.pm.prj.vo.StepVo;
import com.otod.pm.prj.vo.TaskVo;

/**
 * 阶段管理
 * @author jqiu 
 */
@Controller
@RequestMapping("pm/step")
public class StepController extends BaseController {

	@Autowired
	private StepService stepService;
	@Autowired
	private TaskService taskService;
	@Autowired
	private ProjectService projectService;
	
	
	
	/**
	 * 保存阶段
	 * @param stepVo
	 * @return
	 * @throws AppException 
	 */
	@RequestMapping(value ="save", method = RequestMethod.POST)
	@ResponseBody
	public StepVo save(StepVo stepVo,HttpServletRequest request) throws AppException {
		if(StringUtils.isEmpty(stepVo.getUuid())){
			super.setInsertOperInfo(stepVo);
			return stepService.insert(stepVo);
		}else{
			Map<String,Object> map = new HashMap<String, Object>();
			map.put("uuid", stepVo.getUuid());
			map.put("stepName", stepVo.getStepName());
			map.put("startDate", stepVo.getStartDate());
			ProjectVo projectVo = projectService.getById(stepVo.getPrjId());
			if(PmConstant.PRJ_STATUS_STARTING.equals(projectVo.getStatus())){
				
				if(DateUtils.getDaysBetweenTwoDates(DateUtils.formatDate(stepVo.getStartDate(),"yyyyMMdd"),DateUtils.getDate("yyyyMMdd"))<0){
					map.put("status", PmConstant.STEP_STATUS_READY);
				}else{
					map.put("status", PmConstant.STEP_STATUS_STARTING);
				}
			}else{
				map.put("status", PmConstant.STEP_STATUS_READY);
				
			}
			
			map.put("endDate", stepVo.getEndDate());
			super.setUpdateOperInfo(map);
			stepService.update(map);
			return stepVo;
		}
	}
	
	/**
	 * 阶段查询
	 * @param prjId
	 * @return
	 * @throws AppException 
	 */
	@RequestMapping(value ="list", method = RequestMethod.POST)
	@ResponseBody
	public List<StepBo> list(@RequestBody String prjId) throws AppException{
		if(StringUtils.isEmpty(prjId)){
			throw new AppException("产品ID不能为空！");
		}

		List<StepBo> listStepBo = stepService.queryStepList(prjId,"");
		return listStepBo;
	}
	
	/**
	 * 阶段删除
	 * @param uuid
	 * @return
	 */
	@RequestMapping(value ="delete", method = RequestMethod.POST)
	@ResponseBody
	public String delete(@RequestBody String uuid){
		stepService.deleteById(uuid);
		return JsonSuccess.factory("删除成功");
	}
	
	
	/**
	 * 选择项目模板
	 * @param prjId 项目id
	 * @param prjType 项目类型
	 * @param tpId 模板id
	 * @return
	 * @throws AppException
	 */
	@RequestMapping(value ="createPrjStepByTemplate", method = RequestMethod.GET)
	@ResponseBody
	public String createPrjStepByTemplate(String prjId,String prjType,String tpId) throws AppException{
		//prjId项目id
		//tpId 模板id
		//通过模板的id 查询 tp表中数据 然后插入到表pm_t_step
		//首先要查出 StepTp表中的数据
		//通过项目类别查询到tpId
		TaskVo vo = new TaskVo();
		super.setInsertOperInfo(vo);
		stepService.createPrjStepByTemplate(prjId, tpId,vo);
		return JsonSuccess.factory("成功");
	}
	
	/**
	 * 阶段完成
	 * @param uuid
	 * @return
	 */
	@RequestMapping(value ="updateStepComplete", method = RequestMethod.GET)
	@ResponseBody
	public String updateStepComplete(String uuid, String forceUpdate){
		
//		if(!PmConstant.STEP_STATUS_STARTING.equals(stepService.getById(uuid).getStatus())){
//			return JsonFailure.factory("阶段当前状态不能跳转！");
//		}
		
		if(stepService.isStepTaskCmt(uuid)==0){
			return JsonFailure.factory("当前阶段有未开始任务不能跳转！");
		}
		
		if(StringUtils.isEmpty(forceUpdate)||!forceUpdate.equals("true")){
			if(stepService.isStepTaskCmt(uuid)==1){
				return JsonSuccess.factory("有未完成任务是否要强制跳转阶段？");
			}
		}
		stepService.updateStatusCpt(uuid);
		return JsonSuccess.factory("跳转成功");
	}
	
	
	
	
	/**
	 * 修改阶段名字
	 * @param uuid  阶段的id
	 * @param stepName 阶段的名字
	 * @return
	 */
	public String updateStepName(String uuid,String stepName){
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("uuid", uuid);
		map.put("stepName", stepName);	
		super.setUpdateOperInfo(map);
		stepService.update(map);
		return JsonSuccess.factory("修改阶段名称名字成功");
	}
}
